<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 附近麻将机店
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class NearbyShopController extends CommonController
{
    private $dealer_role_id;
    private $admin_user;
    private $is_dealer = 0;
    public function _initialize(){
        parent::_initialize();
        $this->dealer_role_id = C('DEALER_ROLE_ID');
        $this->admin_user = session('LG_USER');
        if($this->admin_user['role_id'] == $this->dealer_role_id ){
            $this->is_dealer = 1;
        }
        if(in_array(ACTION_NAME, ['goods', 'goods_add', 'goods_edit'])){
            $this->assign('dealer_role_id', $this->dealer_role_id);
            $this->assign('admin_user', $this->admin_user);
            $this->assign('is_dealer', $this->is_dealer);
        }
    }


    public function index()
    {
        $model = M('nearby_shop');
        $keyword = I('keyword');
        $member = I('member');
        $where = ['check_status'=>1];
        if ($area = I('area')) {
            $where['province'] = explode('/', $area)['0'];
            $where['city'] = explode('/', $area)['1'];
            $where['district'] = explode('/', $area)['2'];
            $this->assign('areaArr',$area);
        }

        if($member){
            $where['member_grade'] = ['egt', 1];
            $where['member_expire'] = ['egt', time()];
        }

        if(!empty($keyword)){
            $where['shop_name'] = array('like',"%".$keyword."%");
        }

        $Count = $model->where($where)->count();
        $Page = new \Think\Pager($Count,10);
        $show = $Page->showhtml();
        
        $list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        if($list){
            foreach($list as $key=>&$row){
                $row['status'] = $row['status'] ? '开启' : '关闭';
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);
        $this->display();
    }

    public function add()
    {
        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            if(empty($data['picture'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传列表图片'));
            }
            if(empty($data['pictures'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            // $data['picture'] = isset($data['picture'][0]) ? $data['picture'][0] : '';
            // $data['pictures'] = !empty($data['pictures']) ? implode(',', $data['pictures']) : '';
            // $data['boss_img'] = !empty($data['boss_img']) ? implode(',', $data['boss_img']) : '';
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];
            $data['check_status'] = 1;

            $model = M('nearby_shop');

            $user_model = M('user');
            $user = $user_model->where(['phone'=>$data['mobile']])->find();
            if($user){
                $user_id = $user['id'];
            }else{
                $user_id = user_reg([
                    'img'       => C('SiteUrl').'Public/upload/userimg/t.jpg',
                    'nickname'  => "用户".$data['mobile'],
                    'phone'     => $data['mobile'],
                    'pwd'       => md5(123456),
                    'ctime'     => time(),
                ]);
                if(!$user_id){
                    $this->ajaxReturn(array('s'=>false,'m'=>'用户添加失败!'));
                }
            }
            $data['uid'] = $user_id;
            $data['create_time'] = time();

            $data['member_expire'] = strtotime($data['member_expire']);
            if($data['member_expire']===false){
                unset($data['member_expire']);
            }
            $id = $model->add($data);
            if($id){
                if(!empty($data['admin_id'])){
                    M('rbac_manager')->where(['id'=>$data['admin_id']])->save(['nearby_shop_id'=>$id]);
                }
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->dealer_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);
            $this->assign('admin_arr', $admin_arr);
            $this->display();
        }
    }

    public function edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            if(empty($data['picture'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传列表图片'));
            }
            if(empty($data['pictures'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播'));
            }
            $data['member_expire'] = strtotime($data['member_expire']);
            if($data['member_expire']===false){
                unset($data['member_expire']);
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            // $data['picture'] = isset($data['picture'][0]) ? $data['picture'][0] : '';
            // $data['pictures'] = !empty($data['pictures']) ? implode(',', $data['pictures']) : '';
            // $data['boss_img'] = !empty($data['boss_img']) ? implode(',', $data['boss_img']) : '';
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];

            $user_model = M('user');
            $user = $user_model->where(['phone'=>$data['mobile']])->find();
            if($user){
                $user_id = $user['id'];
            }else{
                $user_id = $user_model->add([
                    'img'       => C('SiteUrl').'Public/upload/userimg/t.jpg',
                    'nickname'  => "用户".$data['mobile'],
                    'phone'     => $data['mobile'],
                    'pwd'       => md5(123456),
                    'ctime'     => time(),
                ]);
            }
            $data['uid'] = $user_id;



            $model = M('nearby_shop');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                if(!empty($data['admin_id'])){
                    M('rbac_manager')->where(['id'=>$data['admin_id']])->save(['nearby_shop_id'=>$id]);
                }
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->dealer_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);
            $info = M('nearby_shop')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->assign('admin_arr', $admin_arr);
            $this->display();
        }
    }
    
    public function apply(){
        $model = M('nearby_shop');
        $Count = $model->count();
        $Page = new \Think\Pager($Count, 10);
        $show = $Page->showhtml();
        $keyword = I('keyword');
        $where = ['check_status'=>['in', [0]]];
        if(!empty($keyword)){
            $where['name|mobile'] = array('like',"%".$keyword."%");
        }

        $list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        if($list){
            $status_arr = ['申请中', '同意', '拒绝'];
            foreach($list as $key=>&$row){
                $row['status'] = $status_arr[$row['check_status']];
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);
        $this->display();
    }

    public function apply_edit(){
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            if(empty($data['picture'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传列表图片'));
            }
            if(empty($data['pictures'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            $pcd = explode("/", $data['pcd']);
            if(empty($pcd[0])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            }
            if(empty($pcd[1])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            // $data['picture'] = isset($data['picture'][0]) ? $data['picture'][0] : '';
            // $data['pictures'] = !empty($data['pictures']) ? implode(',', $data['pictures']) : '';
            // $data['boss_img'] = !empty($data['boss_img']) ? implode(',', $data['boss_img']) : '';
            // $data['business_license'] = !empty($data['business_license']) ? implode(',', $data['business_license']) : '';
            $data['province'] = $pcd[0];
            $data['city'] = $pcd[1];
            $data['district'] = $pcd[2];
            if($data['check_status']){
                $data['check_time'] = time();
                if($data['check_status'] == 1){
                    $data['status'] = 1;
                }
            }
            $model = M('nearby_shop');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                if(!empty($data['admin_id'])){
                    M('rbac_manager')->where(['id'=>$data['admin_id']])->save(['nearby_shop_id'=>$id]);
                }
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('apply')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('nearby_shop')->where(['id'=>$id])->find();
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->dealer_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);
            $this->assign('info', $info);
            $this->assign('admin_arr', $admin_arr);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('nearby_shop')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }

    public function goods()
    {
        $model = M('nearby_shop_goods');
        $Count = $model->count();
        $Page = new \Think\Pager($Count, 10);
        $show = $Page->showhtml();
        $keyword = I('keyword');
        $where = [];
        if(!empty($keyword)){
            $where['goods_name'] = array('like', "%".$keyword."%");
        }

        // 如果是经销商
        if($this->is_dealer){
            $where['nearby_shop_id'] = $this->admin_user['nearby_shop_id'];
        }

        $list = $model->alias('g')
                ->join('left join __NEARBY_SHOP__ n on g.nearby_shop_id=n.id')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order('g.id desc')
                ->field('g.*,n.shop_name')
                ->select();
        if($list){
            foreach($list as $key=>&$row){
                $row['is_on_sale'] = $row['is_on_sale'] ? '上架' : '下架';
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);        

        $this->display();
    }

    public function goods_add()
    {
        if(IS_POST){
            $data = I('post.');
            if(empty($data['goods_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入商品名称!'));
            }
            if(empty($data['picture'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片!'));
            }
            // $data['picture'] = isset($data['picture'][0]) ? $data['picture'][0] : '';
            // $data['alipay_qr'] = isset($data['alipay_qr'][0]) ? $data['alipay_qr'][0] : '';
            // $data['wechat_pay_qr'] = isset($data['wechat_pay_qr'][0]) ? $data['wechat_pay_qr'][0] : '';
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';

            $model = M('nearby_shop_goods');

            $data['create_time'] = time();

            $res = $model->add($data);
            if($res !== false){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('goods')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{    
            // 经销商
            $nearby_shop = M('nearby_shop')->where(['status'=>1, 'check_status'=>1])->getField('id,shop_name', true);
            // 品牌
            $brand_arr = M('brand')->where(['check_status'=>1, 'status'=>1])->getField('id,brand_name', true);
            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
            $this->assign('nearby_shop', $nearby_shop);
            $this->assign('brand_arr', $brand_arr);
            $this->display();    
        }
    }

    public function goods_edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['goods_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入商品名称!'));
            }
            if(empty($data['picture'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片!'));
            }
            // $data['picture'] = isset($data['picture'][0]) ? $data['picture'][0] : '';
            // $data['alipay_qr'] = isset($data['alipay_qr'][0]) ? $data['alipay_qr'][0] : '';
            // $data['wechat_pay_qr'] = isset($data['wechat_pay_qr'][0]) ? $data['wechat_pay_qr'][0] : '';
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';

            $model = M('nearby_shop_goods');
            $res = $model->where(['id'=>$id])->save($data);
            if($res !==false ){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('goods')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }            
        }else{
            // 经销商
            $nearby_shop = M('nearby_shop')->where(['status'=>1, 'check_status'=>1])->getField('id,shop_name', true);
            // 品牌
            $brand_arr = M('brand')->where(['check_status'=>1, 'status'=>1])->getField('id,brand_name', true);
            $info = M('nearby_shop_goods')->where(['id'=>$id])->find();
            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
            $this->assign('info', $info);
            $this->assign('nearby_shop', $nearby_shop);
            $this->assign('brand_arr', $brand_arr);       
            $this->display();
        }
    }

    public function goods_del()
    {
        $id = I('id');
        $res = M('nearby_shop_goods')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('goods')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }    
}
